package project3.datasets;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/**
 * Support class for <code>Attribute</code> implementers.
 * 
 * @author Antonio
 * @author Vika
 */
public abstract class AbstractAttribute implements Attribute {

	/** */
	protected int index = 1;

	/** */	
	protected String name;

	/** */	
	protected double weight;
	
	/**
	 * 
	 */
	public AbstractAttribute() {
	}

	/**
	 * 
	 */
	public AbstractAttribute(int index, String name) {
		this(index, name, 1d);
	}

	/**
	 * 
	 */
	public AbstractAttribute(int index, String name, double weight) {
		this.index = index;
		this.name = name;
		this.weight = weight;
	}
	
	@Override
	public int hashCode() {
		int hash = this.index;
		hash = 37 * this.name.hashCode();
        long bits = Double.doubleToLongBits(this.weight);
        hash = 7 * (int)(bits ^ (bits >>> 32));
        return hash;		
	}	
	
	@Override
	public Attribute clone() {
		AbstractAttribute attr = null;
		try {
			attr = (AbstractAttribute) super.clone();
		} catch (CloneNotSupportedException e) {
			throw new Error(e);
		}
		return attr;
	}		
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof AbstractAttribute) {
			AbstractAttribute other = (AbstractAttribute) obj;
			return this.index == other.index &&
				this.name.equals(other.name) &&
					this.weight == other.weight;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see dataset.Attribute#getIndex()
	 */
	public int getIndex() {
		return index;
	}

	/* (non-Javadoc)
	 * @see dataset.Attribute#setIndex(int)
	 */
	public void setIndex(int index) {
		this.index = index;
	}

	/* (non-Javadoc)
	 * @see dataset.Attribute#getName()
	 */
	public String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see dataset.Attribute#setName(java.lang.String)
	 */
	public void setName(String name) {
		this.name = name;
	}

	/* (non-Javadoc)
	 * @see dataset.Attribute#getWeight()
	 */
	public double getWeight() {
		return weight;
	}

	/* (non-Javadoc)
	 * @see dataset.Attribute#setWeight(double)
	 */
	public void setWeight(double weight) {
		this.weight = weight;
	}
	
	/*
	 *  (non-Javadoc)
	 * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
	 */
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeInt(this.index);
		out.writeUTF(this.name);
		out.writeDouble(this.weight);
	}
	
	/*
	 *  (non-Javadoc)
	 * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
	 */
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		this.index = in.readInt();
		this.name = in.readUTF();
		this.weight = in.readDouble();
	}
	
}